package com.wt.admin.config;

import cn.hutool.core.util.StrUtil;
import com.aizuda.easy.security.code.BasicCode;
import com.aizuda.easy.security.domain.Rep;
import com.aizuda.easy.security.exp.IErrorCode;
import com.aizuda.easy.security.exp.impl.BasicException;
import com.wt.admin.config.i18n.MessageHelperConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.util.Map;
import java.util.function.Function;

@ControllerAdvice
@Slf4j
@RestControllerAdvice
@RestController
public class GlobalExceptionConfig {

    Map<Class<?>, Function<Exception,Rep<?>>> expMap = Map.of(
            NoHandlerFoundException.class,e -> Rep.error(BasicCode.BASIC_CODE_404.getCode(), MessageHelperConfig.getMessage(BasicCode.BASIC_CODE_404)),
            IllegalArgumentException.class,e -> Rep.error(BasicCode.BASIC_CODE_99999.getCode(), MessageHelperConfig.getMessage(BasicCode.BASIC_CODE_99999)),
            MissingServletRequestParameterException.class,e -> Rep.error(BasicCode.BASIC_CODE_99998.getCode(), MessageHelperConfig.getMessage(BasicCode.BASIC_CODE_99998)),
            HttpRequestMethodNotSupportedException.class,e -> {
                String msg = e.getMessage();
                msg = MessageHelperConfig.getMessage(BasicCode.BASIC_CODE_99997) +
                        (StrUtil.isEmpty(msg) ? "" : msg.substring(msg.indexOf("'") - 1, msg.lastIndexOf("'") + 1));
                return Rep.error(BasicCode.BASIC_CODE_99997.getCode(), msg);
            },
            BasicException.class,e -> {
                BasicException cause = (BasicException) e;
                return Rep.error(cause.getCode(), MessageHelperConfig.getMessage(cause.getCode()));
            },
            CheckException.class,e -> {
                CheckException cause = (CheckException) e;
                return Rep.error(cause.getCode(), MessageHelperConfig.getMessage(cause));
            }
    );

    /**
     * 处理Exception异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Rep<?> handleAllExceptions(Exception e, WebRequest request) {
        String msg = MessageHelperConfig.getMessage(BasicCode.BASIC_CODE_500);
        e.printStackTrace();
        for (Class<?> next : expMap.keySet()) {
            if (e.getCause() instanceof BasicException cause) {
                return Rep.error(cause.getCode(), MessageHelperConfig.getMessage(cause.getCode()));
            }
            if (e.getCause() instanceof CheckException cause) {
                return Rep.error(cause.getCode(), MessageHelperConfig.getMessage(cause));
            }
            if (!next.isAssignableFrom(e.getClass())) {
                continue;
            }
            return expMap.get(next).apply(e);
        }
        return Rep.error(BasicCode.BASIC_CODE_500.getCode(), msg);
    }

    @RequestMapping("/failure/authenticationFilter")
    public Rep<?> error(@RequestParam("code") Integer code, @RequestParam("msg") String msg) {
        return Rep.error(code, MessageHelperConfig.getMessage(code));
    }

}